<template>
    <div>
        <a-card style="width: 300px">
            <img
                slot="cover"
                :src="detailData.goodsPictures && detailData.goodsPictures.length>0 && detailData.goodsPictures[0] || ''"
                />
            <a-card-meta :title="detailData.goodsName">
                <!-- <div slot="description">
                    &yen;{{detailData.salePrice | formatPrice}}
                </div> -->
            </a-card-meta>
        </a-card>
        <DiscountInventorySettings :readonly="true" :tableData="tableData" @change="handleDiscountInventorySettings"></DiscountInventorySettings>
        <div class="row flex flex_end">
            <a-space>
                <a-button @click="handleCancel">返回</a-button>
            </a-space>
        </div>
        <ChooseGoods :visible="chooseGoodsVisible" @cancel="chooseGoodsVisible=false" @submit="handleChooseGoodsSubmit"></ChooseGoods>
    </div>
</template>

<script>
import DiscountInventorySettings from './components/DiscountInventorySettings';
import ChooseGoods from './components/ChooseGoods';

export default {
    components:{
        DiscountInventorySettings,
        ChooseGoods
    },
    computed:{
        activityId(){
            return this.$route.query.activityId || '';
        },
    },
    data(){
        return {
            productId:'',
            detailData:{}, 
            chooseGoodsVisible:false,           
            tableData:[
                // {
                //     id:1,
                //     isParticipateDiscount:true,//是否参与折扣
                //     commoditySpec:'紫色',//商品规格
                //     stock:1,//现有库存
                //     originPrice:22,//原价（销售价）
                //     discount:3,//打折
                //     reduceMoney:'',//减价
                //     promotionPrice:'',//促销价
                //     promotionMethod:'打折',//促销方式
                //     quantityOrdered:3,//已下单数量
                // },
                // {
                //     id:2,
                //     isParticipateDiscount:true,//是否参与折扣
                //     commoditySpec:'黑色',//商品规格
                //     stock:0,//现有库存
                //     originPrice:26,//原价（销售价）
                //     discount:'',//打折
                //     reduceMoney:'',//减价
                //     promotionPrice:'',//促销价
                //     promotionMethod:'打折',//促销方式
                //     quantityOrdered:10,//已下单数量
                // },
                // {
                //     id:3,
                //     isParticipateDiscount:true,//是否参与折扣
                //     commoditySpec:'红色',//商品规格
                //     stock:11,//现有库存
                //     originPrice:46,//原价（销售价）
                //     discount:'',//打折
                //     reduceMoney:'',//减价
                //     promotionPrice:'',//促销价
                //     promotionMethod:'打折',//促销方式
                //     quantityOrdered:65,//已下单数量
                // }
            ],
            callbackTableData:[],
            isCanSubmit:false,
            loading:false,
        }
    },
    filters:{
        formatPrice(val){
            if(!val)return '0.00';
            return (val/100).toFixed(2);
        }
    },
    created(){
        this.productId=this.$route.query.productId || '';
        this.getProductDetail(this.productId);
    },
    methods:{
        handleChooseGoodsSubmit(id){
            this.chooseGoodsVisible=false;
            this.productId=id;
            this.getProductDetail(this.productId);
        },
        async getProductDetail(id){
            let res=await this.$get('/goods/query',{
                id
            });
            if(res && res.returnCode=='1000'){
                let dataInfo=res.dataInfo || {};
                dataInfo.goodsPictures=dataInfo.goodsPictures.split(',');
                this.detailData=dataInfo;
                this.getListData(this.detailData.goodsCreateType);
            }
        },
        //秒杀库存设置
        async getListData(goodsCreateType){
            let res=await this.$get('goldenhome/seckill/admin_/discount/goodsnorms/list',{
                activityId:this.activityId,
                discountGoodsId:this.productId,
                goodsCreateType
            });
            if(res && res.returnCode=='1000'){                
                this.tableData=await this.apiFieldToCustomField(res.dataInfo);
                console.log('this.tableData',this.tableData)
            }
        },
        async apiFieldToCustomField(data){
            let tmpData=[];
            for(let i=0;i<data.length;i++){
                let item=data[i];
                let normName=await this.getName(item.modelIds);
                
                tmpData.push({
                    id:item.id,
                    isParticipateDiscount:item.enableDiscount==1,//是否参与折扣
                    commoditySpec:normName,//商品规格
                    stock:item.goodsInventory,//现有库存
                    originPrice:((item.salePrice || 0) / 100).toFixed(2),//原价（销售价）
                    discount:item.discount || '',//打折
                    reduceMoney:((item.reducePrice || 0) / 100).toFixed(2),//减价
                    promotionPrice:((item.discountPrice || 0) / 100).toFixed(2),//促销价
                    promotionMethod:'打折',//促销方式
                    quantityOrdered:item.orderCount,//已下单数量

                    createBy:item.createBy,
                    updateBy:item.updateBy,
                    createDate:item.createDate,
                    updateDate:item.updateDate,
                    sort:item.sort,
                    remark:item.remark,
                    deleted:item.deleted,
                    current:item.current,
                    size:item.size,
                    seckillGoodsId:item.seckillGoodsId,
                    normsId:item.normsId,
                    superiorModelIds:item.superiorModelIds,
                    modelIds:item.modelIds,
                })
            };
            return tmpData;
        },
        // 获取商品所有规格名称
        async getModelNormName(id) {
            let res = await this.$get('/goods/modelnorm/id', {
                id
            },false,true)
            if(res && res.returnCode=='1000'){
                return res.dataInfo.normName || ''
            }
        },
        //获取名字
        async getName(modelIds){
            let name='';
            if(!modelIds){
                name = '统一规格';
            }
            else {//多级规格
                if(modelIds){
                    let ids=modelIds.split(',');
                    let nameArr=[];
                    for(let j=0;j<ids.length;j++){
                        let name=await this.getModelNormName(ids[j]);
                        nameArr.push(name);
                    }
                    name = nameArr.join(',');
                }
            }
            return name;
        },
        handleDiscountInventorySettings(data){
            //console.log(data);
            this.callbackTableData=data;
            this.isCanSubmit=!!this.callbackTableData;
        },
        handleCancel(){
            this.$router.push({
                name:'MarketingDiscountCommodityManage',
                query:{
                    activityId:this.activityId,
                }
            })
        },
        handleSave(){
            this.loading=true;
            console.log(this.callbackTableData);
        }
    }
}
</script>

<style lang="less" scoped>
.row{
    margin-top:10px;
    margin-bottom:10px;
}
.flex{
    display: flex;
    align-items: center;
    justify-content: center;
}
.flex_end{
    justify-content: flex-end;
}
</style>